package cn.exrick.sso.service.impl;

import cn.exrick.common.constant.CommonConstant;
import cn.exrick.common.exception.XmallException;
import cn.exrick.common.jedis.JedisClient;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.IDUtil;
import cn.exrick.common.utils.QiniuUtil;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.dto.front.Member;
import cn.exrick.manager.mapper.TbMemberMapper;
import cn.exrick.manager.pojo.TbMember;
import cn.exrick.manager.pojo.TbMemberExample;
import cn.exrick.sso.service.LoginService;
import cn.exrick.sso.service.MemberService;
import cn.exrick.sso.service.RegisterService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private TbMemberMapper tbMemberMapper;
    @Autowired
    private RegisterService registerService;

    @Value("${IMAGE_ACCESS_URL}")
    private String imageUrl;

    @Override
    public TbMember getMemberById(Long id) {
        return tbMemberMapper.selectByPrimaryKey(id);
    }


    @Override
    public Result<Object> changePassword(String email, String password) {
        TbMemberExample example = new TbMemberExample();
        TbMemberExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(CommonConstant.ACCOUNT_STATUS_NORMAL);
        criteria.andEmailEqualTo(email);
        List<TbMember> tbMembers = tbMemberMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(tbMembers)){
            return new ResultUtil<>().setErrorMsg("该邮箱未注册");
        }

        TbMember tbMember = tbMembers.get(0);
        tbMember.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        tbMember.setUpdateTime(new Date());
        tbMemberMapper.updateByPrimaryKey(tbMember);
        return new ResultUtil<>().setData(null);
    }

    @Override
    public Result<Object> modifyPassword(String oldPassword, String password, Long memberId) {
        TbMember tbMember = tbMemberMapper.selectByPrimaryKey(memberId);
        String oldPwdMd5 = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if(!oldPwdMd5.equals(tbMember.getPassword())){
            return new ResultUtil<>().setErrorMsg("原密码不正确");
        }
        String newPwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        tbMember.setPassword(newPwdMd5);
        tbMember.setUpdateTime(new Date());
        tbMemberMapper.updateByPrimaryKey(tbMember);
        return new ResultUtil<>().setData(null);
    }

    @Override
    public Result<Object> updateMemeber(TbMember tbMember) {
        if(!registerService.checkData(tbMember.getUsername(),1,tbMember.getId())){
            return new ResultUtil<>().setErrorMsg("该用户名已被注册");
        }

        if(!registerService.checkData(tbMember.getPhone(),2,tbMember.getId())){
            return new ResultUtil<>().setErrorMsg("该手机号已被注册");
        }

        if(!registerService.checkData(tbMember.getEmail(),3,tbMember.getId())){
            return new ResultUtil<>().setErrorMsg("该邮箱已被注册");
        }

        tbMemberMapper.updateByPrimaryKeySelective(tbMember);
        return new ResultUtil<>().setData(null);
    }
}
